package com.kly.sc.api.application.utils;

import com.alibaba.nacos.common.utils.UuidUtils;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class CreateBucket {
    private static final Logger LOGGER = LoggerFactory.getLogger(CreateBucket.class);
    private String s3Region = "us-west-1";
    private String s3AccessKeyId = "AKIARL4XOJIPZYJ4EPUG";
    private String s3AccessKeySecret = "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";
    private String s3BucketName = "s3test160333-dev";
    public CreateBucket() {
    }

    public static void main(String[] args) {
        CreateBucket CreateBucket = new CreateBucket();
        final String s = CreateBucket.uploadFile("public/" + UuidUtils.generateUuid(), "1212");
        System.out.println("https://s3test160333-dev.s3.us-west-1.amazonaws.com/public/" + s);
    }

    public void uploadFile(String bucketName, InputStream inputStream, long contentLength, String fileName) {
        long start = System.currentTimeMillis();

        try {
            BasicAWSCredentials awsCreds = new BasicAWSCredentials(this.s3AccessKeyId, this.s3AccessKeySecret);
            AmazonS3 s3 = (AmazonS3)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder)AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds))).withRegion(Regions.fromName(this.s3Region))).build();
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentLength(contentLength);
            if (fileName.indexOf(".html") > 0) {
                meta.setContentType("text/html");
            }

            if (fileName.indexOf(".mp3") > 0) {
                meta.setContentType("audio/mp3");
            }

            if (fileName.indexOf(".jpg") > 0 || fileName.indexOf(".gif") > 0 || fileName.indexOf(".png") > 0 || fileName.indexOf(".ico") > 0 || fileName.indexOf(".bmp") > 0 || fileName.indexOf(".jpeg") > 0) {
                meta.setContentType("image/jpeg");
            }

            PutObjectRequest request = new PutObjectRequest(bucketName, fileName, inputStream, meta);
            request.withCannedAcl(CannedAccessControlList.PublicReadWrite);
            s3.putObject(request);
        } catch (Exception var12) {
            LOGGER.error(var12.getMessage(), var12);
            throw var12;
        }

        long time = System.currentTimeMillis() - start;
        LOGGER.info("Cost {} ms to upload the file {}", time, fileName);
    }

    public String uploadFile(String fileName, String content) {
        BasicAWSCredentials awsCreds = new BasicAWSCredentials(this.s3AccessKeyId, this.s3AccessKeySecret);
        AmazonS3 s3 = (AmazonS3)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder)AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds))).withRegion(Regions.fromName(this.s3Region))).build();
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength((long)content.getBytes().length);

        try {
            PutObjectRequest request = new PutObjectRequest(this.s3BucketName, fileName, new ByteArrayInputStream(content.getBytes()), meta);
            request.withCannedAcl(CannedAccessControlList.PublicReadWrite);
            s3.putObject(request);
            return fileName;
        } catch (Exception var7) {
            LOGGER.error(var7.getMessage(), var7);
            throw var7;
        }
    }

    public void uploadFile(InputStream inputStream, long contentLength, String fileName) {
        BasicAWSCredentials awsCreds = new BasicAWSCredentials(this.s3AccessKeyId, this.s3AccessKeySecret);
        AmazonS3 s3 = (AmazonS3)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder)AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds))).withRegion(Regions.fromName(this.s3Region))).build();
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(contentLength);
        PutObjectRequest request = new PutObjectRequest(this.s3BucketName, fileName, inputStream, meta);
        request.withCannedAcl(CannedAccessControlList.PublicRead);

        try {
            s3.putObject(request);
        } catch (Exception var10) {
            LOGGER.error(var10.getMessage(), var10);
            throw var10;
        }
    }

    public void uploadFile(InputStream inputStream, String fileName, String contentType) {
        BasicAWSCredentials awsCreds = new BasicAWSCredentials(this.s3AccessKeyId, this.s3AccessKeySecret);
        AmazonS3 s3 = (AmazonS3)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder)AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds))).withRegion(Regions.fromName(this.s3Region))).build();
        ObjectMetadata meta = new ObjectMetadata();
        if (StringUtils.isNotEmpty(contentType)) {
            meta.setContentType(contentType);
        }

        PutObjectRequest request = new PutObjectRequest(this.s3BucketName, fileName, inputStream, meta);
        request.withCannedAcl(CannedAccessControlList.PublicRead);

        try {
            s3.putObject(request);
        } catch (Exception var9) {
            LOGGER.error(var9.getMessage(), var9);
            throw var9;
        }
    }

    public void uploadFile(String bucketName, InputStream inputStream, long contentLength, String fileName, String contentType) {
        BasicAWSCredentials awsCreds = new BasicAWSCredentials(this.s3AccessKeyId, this.s3AccessKeySecret);
        AmazonS3 s3 = (AmazonS3)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder)AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds))).withRegion(Regions.fromName(this.s3Region))).build();
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(contentLength);
        if (StringUtils.isNotEmpty(contentType)) {
            meta.setContentType(contentType);
        }

        PutObjectRequest request = new PutObjectRequest(this.s3BucketName, fileName, inputStream, meta);
        request.withCannedAcl(CannedAccessControlList.PublicRead);

        try {
            s3.putObject(request);
        } catch (Exception var12) {
            LOGGER.error(var12.getMessage(), var12);
            throw var12;
        }
    }

    public String getFileContent(String bucketName, String fileNamePath) {
        if (bucketName != null && !bucketName.trim().equals("") && fileNamePath != null && !fileNamePath.trim().equals("")) {
            BasicAWSCredentials awsCreds = new BasicAWSCredentials(this.s3AccessKeyId, this.s3AccessKeySecret);
            AmazonS3 s3 = (AmazonS3)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder)AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds))).withRegion(Regions.fromName(this.s3Region))).build();
            InputStream objectContent = null;
            String content = null;

            try {
                S3Object s3Object = s3.getObject(bucketName, fileNamePath);
                objectContent = s3Object.getObjectContent();
                content = new String(this.input2byte(objectContent), "utf-8");
            } catch (IOException var16) {
                LOGGER.error(var16.getMessage(), var16);
            } finally {
                try {
                    if (objectContent != null) {
                        objectContent.close();
                    }
                } catch (IOException var15) {
                }

            }

            return content;
        } else {
            return "";
        }
    }

    public byte[] input2byte(InputStream inStream) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        boolean var4 = false;

        int rc;
        while((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }

        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }

    public InputStream getFileContentInputStream(String bucketName, String fileNamePath) {
        if (bucketName != null && !bucketName.trim().equals("") && fileNamePath != null && !fileNamePath.trim().equals("")) {
            BasicAWSCredentials awsCreds = new BasicAWSCredentials(this.s3AccessKeyId, this.s3AccessKeySecret);
            AmazonS3 s3 = (AmazonS3)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder)AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds))).withRegion(Regions.fromName(this.s3Region))).build();
            S3Object s3Object = null;
            InputStream objectContent = null;
            Object var7 = null;

            try {
                s3Object = s3.getObject(bucketName, fileNamePath);
                objectContent = s3Object.getObjectContent();
            } catch (Exception var17) {
                LOGGER.error(var17.getMessage(), var17);
            } finally {
                try {
                    if (objectContent != null) {
                        objectContent.close();
                    }
                } catch (IOException var16) {
                }

            }

            return objectContent;
        } else {
            return null;
        }
    }
}